package servlet;

import bean.DealRecord;
import bean.PageBean;
import dao.DealRecordDao;
import net.sf.json.JSONObject;
import util.DealRecordUtil;
import util.ResponseUtil;
import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用于控制交易记录一览
 * url示例：/dealrecord?userid=1&start=1&pagesize=2
 *
 * @author dsf
 */
@WebServlet(name = "drServlet", urlPatterns = "/dealrecord")
public class DealRecordServlet extends HttpServlet {

    private DealRecordDao drDao = new DealRecordDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject result = new JSONObject();
        //http状态码
        int status = 403;
        String msg = "failed";
        if (StringUtil.isNotEmpty(req.getParameter("userid"))) {
            //从url中获取参数
            String userId = req.getParameter("userid");
            String startStr = req.getParameter("start");  //当前页
            String pagesizeStr = req.getParameter("pagesize");  //页记录数
            if (StringUtil.isNotEmpty(startStr) && StringUtil.isNotEmpty(pagesizeStr)) {
                //分页查询
                int start = 0;
                int pagesize = 0;
                try {
                    start = Integer.parseInt(startStr);
                    pagesize = Integer.parseInt(pagesizeStr);
                    PageBean pageBean = new PageBean(start, pagesize);
                    //设置查询参数
                    DealRecord drSearchPara = new DealRecord();
                    drSearchPara.setUserId(userId);
                    drSearchPara.setAlipayStatus("1and2");
                    List<Object> drSearchRes;
                    try {
                        //获得查询结果集
                        drSearchRes = drDao.dealrecordSearch(pageBean, drSearchPara, "OR");
                        //查询成功或失败的对应处理
                        status = 200;
                        msg = "success";
                        //查询成功向前端返回对应数据
                        result = DealRecordUtil.drJson(drSearchRes, status, msg);
                    } catch (Exception e) {
                        result.put("status", status);
                        result.put("msg", msg);
                        e.printStackTrace();
                    }
                } catch (NumberFormatException e) {
                    result.put("status", status);
                    result.put("msg", msg);
                    e.printStackTrace();
                    System.out.println("error pagesize or start");
                }
            } else {
                // 查看今日消费与充值
                result.put("status", 200);
                result.put("msg", "success");
                result.put("data", JSONObject.fromObject(drDao.todayConsumeAndRecharge(userId)));
            }
        } else {
            //数据不合法导致的错误
            result.put("status", status);
            result.put("msg", msg);
        }
        ResponseUtil.write(resp, result);
    }
}